package com.chat.model;

import org.springframework.web.socket.WebSocketSession;

import java.time.LocalDateTime;

/**
 * 用户会话数据模型
 * 用于管理WebSocket会话和用户信息
 */
public class UserSession {
    
    /**
     * 会话ID（WebSocket会话ID）
     */
    private String sessionId;
    
    /**
     * 用户名
     */
    private String username;
    
    /**
     * 加入时间
     */
    private LocalDateTime joinTime;
    
    /**
     * WebSocket会话对象
     */
    private WebSocketSession webSocketSession;
    
    /**
     * 用户状态：ONLINE(在线), OFFLINE(离线)
     */
    private String status;
    
    /**
     * 默认构造函数
     */
    public UserSession() {
        this.joinTime = LocalDateTime.now();
        this.status = "ONLINE";
    }
    
    /**
     * 构造函数
     */
    public UserSession(String sessionId, String username, WebSocketSession webSocketSession) {
        this.sessionId = sessionId;
        this.username = username;
        this.webSocketSession = webSocketSession;
        this.joinTime = LocalDateTime.now();
        this.status = "ONLINE";
    }
    
    /**
     * 检查会话是否有效（WebSocket连接是否打开）
     */
    public boolean isValid() {
        return webSocketSession != null && webSocketSession.isOpen();
    }
    
    /**
     * 获取会话持续时间（分钟）
     */
    public long getSessionDurationMinutes() {
        return java.time.Duration.between(joinTime, LocalDateTime.now()).toMinutes();
    }
    
    // Getter和Setter方法
    public String getSessionId() {
        return sessionId;
    }
    
    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public LocalDateTime getJoinTime() {
        return joinTime;
    }
    
    public void setJoinTime(LocalDateTime joinTime) {
        this.joinTime = joinTime;
    }
    
    public WebSocketSession getWebSocketSession() {
        return webSocketSession;
    }
    
    public void setWebSocketSession(WebSocketSession webSocketSession) {
        this.webSocketSession = webSocketSession;
    }
    
    public String getStatus() {
        return status;
    }
    
    public void setStatus(String status) {
        this.status = status;
    }
    
    @Override
    public String toString() {
        return "UserSession{" +
                "sessionId='" + sessionId + '\'' +
                ", username='" + username + '\'' +
                ", joinTime=" + joinTime +
                ", status='" + status + '\'' +
                '}';
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        UserSession that = (UserSession) obj;
        return sessionId != null ? sessionId.equals(that.sessionId) : that.sessionId == null;
    }
    
    @Override
    public int hashCode() {
        return sessionId != null ? sessionId.hashCode() : 0;
    }
}